/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.Random;

import junit.framework.TestCase;





/**
 * Performs Validation Test for url validations.
 *
 * @version $Revision: 1128446 $ $Date: 2011-05-27 13:29:27 -0700 (Fri, 27 May 2011) $
 */
public class UrlValidatorTest extends TestCase {

   private boolean printStatus = false;
   private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.

   public UrlValidatorTest(String testName) {
      super(testName);
   }

   
   
   public void testManualTest()
   {
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   System.out.println("The following should all be true:\n");
	   
	   System.out.print(urlVal.isValid("http://www.amazon.com")); //valid
	   System.out.println("\thttp://www.amazon.com");
	   
	   System.out.print(urlVal.isValid("http://www.amazon.net")); //valid
	   System.out.println("\thttp://www.amazon.net");
	   
	   System.out.print(urlVal.isValid("http://www.amazon.cc")); //valid
	   System.out.println("\thttp://www.amazon.cc");
	   
	   System.out.print(urlVal.isValid("ftp://www.amazon.com")); //valid
	   System.out.println("\tftp://www.amazon.com");
	   
	   System.out.print(urlVal.isValid("h3t://www.amazon.com")); //valid
	   System.out.println("\th3t://www.amazon.com");
	   
	   System.out.print(urlVal.isValid("www.amazon.com")); //valid
	   System.out.println("\twww.amazon.com");
	   
	   System.out.print(urlVal.isValid("amazon.com")); //valid
	   System.out.println("\tamazon.com");
	   
	   System.out.print(urlVal.isValid("http://255.255.255.255")); //valid
	   System.out.println("\thttp://255.255.255.255");
	   
	   System.out.print(urlVal.isValid("http://1.2.3.4")); //valid 
	   System.out.println("\thttp://1.2.3.4");
	   
	   System.out.print(urlVal.isValid("http://0.0.0.0")); //valid 
	   System.out.println("\thttp://0.0.0.0");
	   
	   System.out.print(urlVal.isValid("http://sportsillustrated.cnn.com/mlb/")); //valid 
	   System.out.println("\thttp://sportsillustrated.cnn.com/mlb/");
	   
	   System.out.println("\nThe following should all be false:\n");
	   
	   System.out.print(urlVal.isValid("http:/www.amazon.com")); //invalid
	   System.out.println("\thttp:/www.amazon.com");
	   
	   System.out.print(urlVal.isValid("http://www.amazon")); //invalid
	   System.out.println("\thttp://www.amazon");
	   
	   System.out.print(urlVal.isValid("://www.amazon.com")); //invalid
	   System.out.println("\t://www.amazon.com");
	   
	   System.out.print(urlVal.isValid("http://www.amazon.c")); //invalid
	   System.out.println("\thttp://www.amazon.c");
	   
	   System.out.print(urlVal.isValid("http://256.256.256.256")); //invalid, can't have higher than 255
	   System.out.println("\thttp://256.256.256.256");
	   
	   System.out.print(urlVal.isValid("http://1.2.3.4.5")); //invalid, can only have 4 octets
	   System.out.println("\thttp://1.2.3.4.5");
	   
	   System.out.print(urlVal.isValid("http://")); //invalid
	   System.out.println("\thttp://");
	   
	   System.out.print(urlVal.isValid("http://-1.-1.-1.-1")); //valid 
	   System.out.println("\thttp://-1.-1.-1.-1");
	   
	   
	   
	   
   }
   
   // URL
   public void testYourFirstPartition()
   {
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   System.out.println("\nURL Test:\n");
	   
	   String pro = null;
	   String sub = null;
	   String dom = null;
	   String top = null;
	   String path = null;
	   String port = null;
	   String query = null;
	   
	   for(int p=1; p<=6; p++)
	   {
		   switch(p){
		   case 1: pro = "HTTP://";
		   break;
		   case 2: pro = "FTP://";
		   break;
		   case 3: pro = "HTTPS://";
		   break;
		   case 4: pro = "SFTP://";
		   break;
		   case 5: pro = "H3T://";
		   break;
		   case 6: pro = "";
		   break;
		   }
		   
		   for(int s=1;s<=3;s++)
		   {
			   switch(s){
			   case 1: sub = "www.";
			   break;
			   case 2: sub = "shop.";
			   break;
			   case 3: sub = "1234.";
			   break;
			   }
			   
			   for(int d=1;d <=1; d++)
			   {
				   switch(d){
				   case 1: dom = "google";
				   break;
				   }
				   
				   for(int t=1; t <=5; t++)
				   {
					   switch(t){
					   case 1: top = ".com";
					   break;
					   case 2: top = ".au";
					   break;
					   case 3: top = ".org";
					   break;
					   case 4: top = ".fr";
					   break;
					   case 5: top = ".biz";
					   break;
					   }
					   
					   for(int pa=1; pa <=4; pa++)
					   {
						   switch(pa){
						   case 1: path = "/music";
						   break;
						   case 2: path = "/mus1c";
						   break;
						   case 3: path = "/1234";
						   break;
						   case 4: path = "";
						   break;
						   }
						   
						   for(int po=1; po<=5 ; po++)
						   {
							   switch(po){
							   case 1: port = ":0";
							   break;
							   case 2: port = ":25";
							   break;
							   case 3: port = ":100";
							   break;
							   case 4: port = ":2541";
							   break;
							   case 5: port = "";
							   break;
							   }
							   
							   for(int q=1; q<=3; q++)
							   {
								   switch(q){
								   case 1: query = "?file=file4";
								   break;
								   case 2: query = "?case=124&&file=openMe";
								   break;
								   case 3: query = "";
								   break;  
								   }
								   
								   System.out.print("\n");
								   System.out.print(urlVal.isValid(pro + sub + dom + top + path + port + query));
								   System.out.print("\t");
								   System.out.print(pro + sub + dom + top + path + port + query);
							   }						 
						   }					   
					   }					   
				   }
			   }
		   }
	   }
   }
   
   // IP Address
   public void testYourSecondPartition(){
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   System.out.println("\nIP Address test:\n");
	   
	   int numberOfip = 0;
	   String ip = null;
	   String port = null;
	   String totalip = null;
	   
	   for ( int n = 1; n <= 3; n++)
	   {
		   switch(n){
		   case 1: numberOfip = 3;
		   break;
		   case 2: numberOfip = 4;
		   break;
		   case 3: numberOfip = 5;
		   break; 
		   }
		   
		   for ( int i = 1; i <= 5; i++)
		   {
			   switch(i){
			   case 1: ip = "-1";
			   break;
			   case 2: ip = "0";
			   break;
			   case 3: ip = "64";
			   break;  
			   case 4: ip = "255";
			   break; 
			   case 5: ip = "256";
			   break; 
			   }
			   
			   switch(n){
			   case 1: totalip = "http://" + ip + "." + ip + "." + ip;
			   break;
			   case 2: totalip = "http://" + ip + "." + ip + "." + ip + "." + ip;
			   break;
			   case 3: totalip = "http://" + ip + "." + ip + "." + ip + "." + ip + "." + ip;
			   break;  
			   }
			   
			   for ( int p = 1; p <= 8; p ++)
			   {
				   switch(p){
				   case 1: port = "";
				   break;
				   case 2: port = ":0";
				   break;
				   case 3: port = ":25";
				   break;  
				   case 4: port = ":100";
				   break; 
				   case 5: port = ":255";
				   break; 
				   case 6: port = ":6589";
				   break; 
				   case 7: port = ":65535";
				   break; 
				   case 8: port = ":65536";
				   break; 
				   }
				   
				   System.out.print("\n");
				   System.out.print(urlVal.isValid(totalip + port));
				   System.out.print("\t");
				   System.out.print(totalip + port);
			   }
		   }
	   }
	   
   }
   
   public void testIsValid()
   {
	   ResultPair[] schemePair = {new ResultPair("http://", true),
   					new ResultPair("ftp://", true),
   					new ResultPair("https://", true),
   					new ResultPair("http:/", false),
   					new ResultPair("http:", false),
   					new ResultPair("h3t://", true),
   					new ResultPair("ht3://", false),
   					new ResultPair("://", false),
   					new ResultPair("abcdefgh://", false),
   					new ResultPair("", true)};
	   
	   ResultPair[] domainPair = {new ResultPair("www.google.com", true),
					new ResultPair("google.com", true),
					new ResultPair("amazon.au", true),
					new ResultPair("amazon.co.uk", true),
					new ResultPair("google.com.hk", true),
					new ResultPair("0.0.0.0", true),
					new ResultPair("255.255.255.255", true),
					new ResultPair(".amazon", false),
					new ResultPair("amazon.", false),
					new ResultPair("amazon", false),
					new ResultPair("1.2.3", false),
					new ResultPair("1.2.3.4.5", false),
					new ResultPair("www.amazon.c", false),
					new ResultPair("256.256.256.256", false),
					new ResultPair("", false)};
	   
	   ResultPair[] pathPair = {new ResultPair("/music", true),
					new ResultPair("/1234", true),
					new ResultPair("/$11", true),
					new ResultPair("/test/", true),
					new ResultPair("", true),
					new ResultPair("/test/test1", true),
					new ResultPair("/..", false),
					new ResultPair("/../", false),
					new ResultPair("/test//test1", false)};
	   
	   ResultPair[] portPair = {new ResultPair("", true),
					new ResultPair(":0", true),
					new ResultPair(":70", true),
					new ResultPair(":12345", true),
					new ResultPair(":-1", false),
					new ResultPair(":60a", false)};
	   
	   Random rand = new Random();
	   ResultPair scheme = null;
	   ResultPair domain = null;
	   ResultPair path = null;
	   ResultPair port = null;
	   
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   System.out.println("URL Test:");
	   
	   for(int i = 0;i<10000;i++)
	   {   
		   boolean validStatus = false;
		   
		   scheme = schemePair[rand.nextInt(schemePair.length)];
		   domain = domainPair[rand.nextInt(domainPair.length)];
		   path = pathPair[rand.nextInt(pathPair.length)];
		   port = portPair[rand.nextInt(portPair.length)];
		   
		   if (scheme.valid && domain.valid && path.valid && port.valid) {
			   validStatus = true;
			   //System.out.print(validStatus);
		   }
		   else {
			   validStatus = false;
			   //System.out.print(validStatus); 
		   }
			  
		   
		   String possibleUrl = scheme.item + domain.item + path.item + port.item;
		   boolean validatorStatus = urlVal.isValid(possibleUrl);
		   if (validatorStatus == validStatus){
			   //Do nothing, verified successfully
		   }
		   else {
			   System.out.println(possibleUrl);
		   }
		   
		   //System.out.print("\t" + urlVal.isValid(possibleUrl));
		   //System.out.print("\t" + possibleUrl + "\n");
		   
		   
	   }
   }
   
   public void testAnyOtherUnitTest()
   {
	   
   }
   /**
    * Create set of tests by taking the testUrlXXX arrays and
    * running through all possible permutations of their combinations.
    *
    * @param testObjects Used to create a url.
    */
   

}
